package servlet;

import dao.EquipmentDAO;
import entity.EquipmentBean;
import entity.Page;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import utils.DBUtil_Plus;
import utils.StrFilter;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/group/equipment/Equipment_list")
public class EquipmentListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public EquipmentListServlet(){
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        this.doPost(request, response);
    }
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
//            ServletException,IOException  {
//        String eq_number = request.getParameter("eq_number");
//        String eq_type = request.getParameter("eq_type");
//        String eq_producer = request.getParameter("eq_producer");
//        String eq_after_sale = request.getParameter("eq_after_sale");
//        String eq_data = request.getParameter("eq_data");
//        // 后面可以借助HashMap来优化一下
//        List<EquipmentBean> eq_list = null;
//        try {
//            eq_list = new ArrayList<EquipmentBean>();
//            Connection conn = DBUtil_Plus.getConnection();
//            String selectSql = "select * from eq_list where 1 = 1";
//            String sql = "";
//            if (eq_number != null && !"".equals(eq_number)) {
//                sql += " and eq_number like ?";
//            }
//            if (eq_producer != null && !"".equals(eq_producer)) {
//                sql += " and eq_producer like ?";
//            }
//            if (eq_data != null && !"".equals(eq_data)) {
//                sql += " and eq_data like ?";
//            }
//            if (eq_after_sale != null && !"".equals(eq_after_sale)) {
//                sql += " and eq_after_sale like ?";
//            }
//            if (eq_type != null && !"".equals(eq_type)) {
//                sql += " and eq_type = ?";
//            }
//            // 组装计数的sql语句
//            PreparedStatement pstmt = conn.prepareStatement(selectSql + sql);
//            int paramIndex = 1;
//            if (eq_number != null && !"".equals(eq_number)) {
//                pstmt.setString(paramIndex++, "%" + eq_number + "%");
//            }
//            if (eq_producer != null && !"".equals(eq_producer)) {
//                pstmt.setString(paramIndex++, "%" + eq_producer + "%");
//            }
//            if (eq_data != null && !"".equals(eq_data)) {
//                pstmt.setString(paramIndex++, "%" + eq_data + "%");
//            }
//            if (eq_after_sale != null && !"".equals(eq_after_sale)) {
//                pstmt.setString(paramIndex++, "%" + eq_after_sale + "%");
//            }
//            if (eq_type != null && !"".equals(eq_type)) {
//                pstmt.setString(paramIndex, eq_type);
//            }
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                EquipmentBean obj = new EquipmentBean();
//                obj.setEq_id(rs.getInt("id"));
//                obj.setEq_name(rs.getString("eq_name"));
//                obj.setEq_number(rs.getString("eq_number"));
//                obj.setEq_type(rs.getString("eq_type"));
//                obj.setEq_producer(rs.getString("eq_producer"));
//                obj.setEq_data(rs.getString("eq_data"));
//                obj.setEq_store_data(rs.getString("eq_store_data"));
//                obj.setEq_after_sale(rs.getString("eq_after_sale"));
//                obj.setEq_phone(rs.getString("eq_phone"));
//                obj.setEq_introduction(rs.getString("eq_introduction"));
//                eq_list.add(obj);
//            }
//            rs.close();
//            pstmt.close();
//            DBUtil_Plus.closeConnection(conn);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        // 在这里用setAttribute定义的但是在list页面中用getParameter也能接受到
//        request.setAttribute("eq_list",eq_list);
//        request.setAttribute("eq_number",eq_number);
//        request.setAttribute("eq_type",eq_type);
//        request.setAttribute("eq_producer",eq_producer);
//        request.setAttribute("eq_after_sale",eq_after_sale);
//        request.setAttribute("eq_data",eq_data);
//        request.getRequestDispatcher("equipment_list.jsp").forward(request,response);
//    }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException,IOException  {

        HttpSession session = request.getSession();
        String pre_eq_number = (String) session.getAttribute("pre_eq_number");
        String pre_eq_type = (String) session.getAttribute("pre_eq_type");
        String pre_eq_producer = (String) session.getAttribute("pre_eq_producer");
        String pre_eq_after_sale = (String) session.getAttribute("pre_eq_after_sale");
        String pre_eq_data = (String) session.getAttribute("pre_eq_data");


    String eq_number = StrFilter.filter(request.getParameter("eq_number"));
    String eq_type = StrFilter.filter(request.getParameter("eq_type"));
    String eq_producer = StrFilter.filter(request.getParameter("eq_producer"));
    String eq_after_sale = StrFilter.filter(request.getParameter("eq_after_sale"));
    String eq_data = StrFilter.filter(request.getParameter("eq_data"));

    Page equipmentPage = (Page) session.getAttribute("equipmentPage");
    // 检查查询条件是否已更新
    if(!eq_number.equals(pre_eq_number) || !eq_type.equals(pre_eq_type) ||
            !eq_producer.equals(pre_eq_producer) || !eq_data.equals(pre_eq_data) ||
            !eq_after_sale.equals(pre_eq_after_sale)){
        equipmentPage = null;
    }
    if(equipmentPage == null){
        equipmentPage = new Page();
        session.setAttribute("equipmentPage",equipmentPage);
    }
    if(request.getParameter("currentPage") != null){
        equipmentPage.setCurrentPage(Integer.parseInt(request.getParameter("currentPage")));
    }

    EquipmentDAO dao = new EquipmentDAO();
    List<EquipmentBean> eq_list = null;
    try {
        eq_list = dao.findEquipment(eq_number,eq_producer, eq_data,eq_after_sale,eq_type,equipmentPage);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    session.setAttribute("pre_eq_number",eq_number);
    session.setAttribute("pre_eq_type",eq_type);
    session.setAttribute("pre_eq_producer",eq_producer);
    session.setAttribute("pre_eq_after_sale",eq_after_sale);
    session.setAttribute("pre_eq_data",eq_data);


    request.setAttribute("eq_list",eq_list);
    request.setAttribute("eq_number",eq_number);
    request.setAttribute("eq_type",eq_type);
    request.setAttribute("eq_producer",eq_producer);
    request.setAttribute("eq_after_sale",eq_after_sale);
    request.setAttribute("eq_data",eq_data);

    request.setAttribute("equipmentPage",equipmentPage);

    request.getRequestDispatcher("equipment_list.jsp").forward(request,response);
}
}
